package com.yuyou.hxyy.service.api.service.impl;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import com.yuyou.hxyy.service.api.exception.BusinessException;
import com.yuyou.hxyy.service.api.service.IQiniuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.UUID;

@Service
public class QiniuServiceImpl implements IQiniuService {



    @Value("${qiniu.ak}")
    private String qiniu_ak;

    @Value("${qiniu.sk}")
    private String qiniu_sk;

    @Value("${qiniu.store.bucket}")
    private String qiniu_store_bucket;

    private Auth auth;

    private String bucket;

    private String cdn = "http://static.yuyou100.com/" ;

    @PostConstruct
    public void init() throws Exception {
        auth = Auth.create(qiniu_ak, qiniu_sk);
        bucket = qiniu_store_bucket;
    }

    public Auth getAuth() {
        return auth;
    }

    public String getBucket() {
        return bucket;
    }

    @Override
    public String upload(InputStream inputStream,String type ) {
            /*int index;
            byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = null;
        try {
            downloadFile = new FileOutputStream("/Users/yuan/Desktop/111.png");

            while ((index = inputStream.read(bytes)) != -1) {
                downloadFile.write(bytes, 0, index);
                downloadFile.flush();
            }
            downloadFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }*/


        Configuration cfg = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(cfg);
        StringMap stringMap = new StringMap();
        String key = UUID.randomUUID().toString().replaceAll("-", "");
        stringMap.put("saveKey", key);
        if("voice".equals(type)){//转mp3
            stringMap.put("persistentPipeline", "yuyou1");
            stringMap.put("persistentOps", "avthumb/mp3/ab/128k/ar/44100/acodec/libmp3lame|saveas/" + UrlSafeBase64.encodeToString(bucket + ":" + key + ".mp3")) ;
        }

        String upToken = auth.uploadToken(bucket,null, 1800, stringMap );
        try {
//            System.out.println(upToken);
            Response response = uploadManager.put(inputStream, null, upToken, null, null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return  cdn +  putRet.key + ("voice".equals(type) ? ".mp3" : StringUtils.EMPTY );
        } catch (QiniuException ex) {
            System.out.println(ex.getMessage());
            throw new BusinessException("上传失败",ex);
        }
    }
}
